import express from 'express'
import { graphqlHTTP } from 'express-graphql'
import bodyParser from 'body-parser'
import schema from './MyGraphQLSchema.js'
import Upload from './Upload.js'

import {
  graphql
} from 'graphql';

const app = express()

// 解析post提交的数据
// urlencoded用来处理post表单提交的数据
app.use(bodyParser.urlencoded({
  extended: false,
  limit: '1024mb'
}))


// 作用是打开 graphql调试页面
app.use(
  '/graphql',
  graphqlHTTP({
    schema,
    graphiql: true
  })
)

app.use('/',Upload)
// 接口 查询
app.get('/api/movies', (req, res, next) => {
  let query = `
    {
      movies {
        id,
        adIndex,
        type,
        title,
        genres,
        theaters {
          id,
          tag,
          adIndex,
          header {
            id,
            textAlign,
            actionUrl,
            icon,
            description,
            title,
            iconType
          }
        }
      }
      
    }
  `
  // res.send(query)
  graphql(schema, query).then(result => {
    res.json(result.data)
  })
})

app.use(express.static('./'))

app.listen(4000)